package com.example.wiki.service;


import com.example.wiki.domain.Category;
import com.example.wiki.domain.CategoryExample;
import com.example.wiki.domain.CategorySaveReq;
import com.example.wiki.mapper.CategoryMapper;
import com.example.wiki.req.CategoryQueryReq;

import com.example.wiki.resp.CategoryQueryResp;
import com.example.wiki.resp.PageResp;
import com.example.wiki.utils.CopyUtil;
import com.example.wiki.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CategoryService {
    private static final Logger Log = LoggerFactory.getLogger(CategoryService.class);
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;


    public List<CategoryQueryResp> all() {



        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        //列表复制
        List<CategoryQueryResp> respList = CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        return respList;
    }

    /**
     * 查询列表
     * @param req
     * @return
     */
    public PageResp<CategoryQueryResp> list(CategoryQueryReq req) {
        System.out.println("req"+req);


        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();


        PageHelper.startPage(req.getPage(),req.getSize());
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        Log.info("总行数"+pageInfo.getTotal());
        Log.info("总页数"+pageInfo.getPages());

//        List<CategoryResp> respList  =new ArrayList<>();
//        for (Category category :categoryList){
//
//            CategoryResp categoryResp = new CategoryResp();
//            //最原始的设置属性值方法
////            categoryResp.setName(category.getName());
////            利用beanutil的简便方法
////            BeanUtils.copyProperties(category,categoryResp);
////            respList.add(categoryResp);
//        }
        //利用工具封装，简化上面代码

        List<CategoryQueryResp> respList = CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        PageResp<CategoryQueryResp> pageResp =new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);
        return pageResp;
    }


        /**
         * 保存
         */


        public void save(@RequestBody CategorySaveReq req) {
            System.out.println(req);
            Category category = CopyUtil.copy(req, Category.class);
            if (ObjectUtils.isEmpty(req.getId())) {
                // 新增  自增 uuid 雪花
                category.setId(snowFlake.nextId());

                categoryMapper.insert(category);
            } else {
                // 更新
                categoryMapper.updateByPrimaryKey(category);
            }
        }

        public void delete(Long id){
            categoryMapper.deleteByPrimaryKey(id);
        }


    }

